package com.zhugang.stack;

/**
 * @program algorithms
 * @description: 基于数组实现的栈
 * @author: chanzhugang
 * @create: 2022/02/22 00:26
 */
public class StackOnArray {

    private final String[] array;
    private final int n;
    private int count;


    public StackOnArray(int n) {
        this.array = new String[n];
        this.n = n;
        this.count = 0;
    }

    /**
     * 入栈
     *
     * @param data
     * @return
     */
    public boolean push(String data) {
        if (count == n) {
            // 栈满
            return false;
        }
        array[count] = data;
        count++;
        return true;
    }

    /**
     * 出栈
     *
     * @return
     */
    public String pop() {
        if (count == 0) {
            // 栈空
            return null;
        }
        String data = array[count - 1];
        count--;
        return data;
    }

}